
var MOD_CLD={
  _diffcl:0,
  _diffver:1,
  _diffObjs:"",
  _sortby:"=",
  claimdict:[], 
  claimdictkw:[],
  _currkw:"",
  _clclaimdict:[],
  _versclaimdict:[],
  impver:1,

  _tfdiffObjs:[],
  _tfdiffcl:0,
  _tfdiffver:1,
  _tfdifftf:0,
  TFVIEW:[],

  printhook:printdiffview
};



MOD_CLD.sortleastdel=function(obj1,obj2) {
  return obj1.strdel - obj2.strdel;
}

MOD_CLD.sortleastins=function(obj1,obj2) {
  return obj1.strins - obj2.strins;
}

MOD_CLD.sortmosteq=function(obj1,obj2) {
  return obj2.streq - obj1.streq;
}

MOD_CLD.sortmosteqprop=function(obj1,obj2) {
  //alert(obj1.strcl);
  return (obj2.streq - obj1.streq)/(obj2.strcl2.length || 1);
}

MOD_CLD.sortleastkw=function(kw1,kw2) {
  //alert(kw1+" "+kw2+" "+claimdict[kw1].length+" "+claimdict[kw2].length); 
  return (MOD_CLD.claimdict[kw1].length ) - (MOD_CLD.claimdict[kw2].length );
}


MOD_CLD.sortbestmatch=function(cl,ord) {
  //alert("S1");
  switch(ord) {
  case "+":
    MOD_CLD._diffObjs.sort(MOD_CLD.sortleastins);
    break;
  case "-":
    MOD_CLD._diffObjs.sort(MOD_CLD.sortleastdel);
    break;
  case "%":
    MOD_CLD._diffObjs.sort(MOD_CLD.sortmosteqprop);
    break; 
  case "=":
    MOD_CLD._diffObjs.sort(MOD_CLD.sortmosteq);
    break;
  }
  //alert("S2");
  SetClaimDiff(cl,MOD_CLD._diffObjs[0].cldiff);
  //alert("S3");
  //alert(_diffObjs[0].diffcl+" "+GetClaimDiff(cl));
  MOD_CLD._sortby=ord;
  edit_claimsetview();
}


MOD_CLD._findbestkeyword=function(cl,vers) {
  var lang=GetDossierLang().toLowerCase();
  var text_iter= new text_iterator(GetClaimDescription(cl)," \n\t;:().,-");  
  MOD_CLD._clclaimdict=cl;
  MOD_CLD._versclaimdict=vers;
  MOD_CLD.claimdict= new Array();
  MOD_CLD.claimdictkw= new Array();
  // build claim dictionary
  while(!text_iter.end()) {
    var kw=text_iter.getValue().toUpperCase();
    if(!kw_isskip(kw,lang)){
      if(kw.length > 2 && !parseInt(kw) && !MOD_CLD.claimdict[kw]) {
	MOD_CLD.claimdictkw.push(kw);
	MOD_CLD.claimdict[kw]= new Array();
      }
    }
    text_iter.next();
  }


  var desc;
  // add list of claims to keywords
  for(var keyw in MOD_CLD.claimdict) {
    var reg=new RegExp(keyw,"i");
    var cldiff=1;
    while(cldiff <= GetClaimSetLength(vers)) {
      if(vers != GetClaimSetVersion() || cldiff != cl) { // skip obvious  
	desc=GetClaimDescription(cldiff,vers);	
	if(desc.search(reg)!=-1) {
	  MOD_CLD.claimdict[keyw].push(cldiff);
	}
      }
      cldiff++;
    }    
  }
  // sort according to number of claims 
  MOD_CLD.claimdictkw.sort(MOD_CLD.sortleastkw);

  // find firts kw with non empty claim list
  for(var idx=0; idx<MOD_CLD.claimdictkw.length; idx++) {
    if(MOD_CLD.claimdict[MOD_CLD.claimdictkw[idx]].length>=1) {
      MOD_CLD._currkw=MOD_CLD.claimdictkw[idx];
      break;
    }
  }
 
}

MOD_CLD.findbestkeyword=function(cl,vers) {
  MOD_CLD._findbestkeyword(cl,vers);
  edit_claimsetview();
}

function keymatch_iterator() {
  this.base = array_iterator;
  this.base(MOD_CLD.claimdictkw);
}
keymatch_iterator.prototype= new array_iterator;
keymatch_iterator.prototype.constructor=keymatch_iterator;
keymatch_iterator.prototype.getValue = function () { return this.getElem().toLowerCase();}

function clkeymatch_iterator() {
  this.base = array_iterator;
  this.base(MOD_CLD.claimdict[MOD_CLD._currkw.toUpperCase()]);
}
clkeymatch_iterator.prototype= new array_iterator;
clkeymatch_iterator.prototype.constructor=clkeymatch_iterator;

MOD_CLD.onchangekw=function() {
  MOD_CLD._currkw=html_selected_iter("selkwcl").toUpperCase();
  edit_claimsetview();
}

//---------------------------------------------------------------------------------------
// 
//                           tf differences 
//
//---------------------------------------------------------------------------------------

MOD_CLD.tf_swfullview=function(cl,state) {
  MOD_CLD.TFVIEW[cl]=state;
  edit_claimsetview();
}

MOD_CLD.exittffindbest=function() {  
  MOD_CLD._tfdiffcl="";
  MOD_CLD._tfdiffver="";
  MOD_CLD._tfclclaimdict="";
  MOD_CLD._tfversclaimdict="";
  edit_claimsetview();
}

MOD_CLD.onchangetfdiff=function(cl,tf,val){
  if(val) {
    var ref= new claim_vtf(val);
    ref.setver(GetRefDiff());
    SetFeatureDiff(cl,tf,ref.getValue());
  } else {
    SetFeatureDiff(cl,tf,html_selected_iter("_difftfcl"));
  }
  //alert(GetFeatureDiff(cl,tf));
  edit_claimsetview();
}

MOD_CLD.tfdiff_seteq=function(cl,tf,val){
  if(val) {
    featurechangeeq(cl,tf,val);
  } else {
    featurechangeeq(cl,tf,GetFeatureDiff(cl,tf));
  }
  document.getElementById("diff_tfeq"+cl+"_"+tf).value=GetFeatureEq(cl,tf);
}


MOD_CLD.tfdiff_import=function(cl,tf,ivtf) {
  CORRUPTEDDOS=1;
  if(ivtf) {
    featurechangeimp(cl,tf,ivtf);
    if(document.getElementById("tfdifftxt"+cl+"_"+tf)) {
      document.getElementById("tfdifftxt"+cl+"_"+tf).value=MOD_CLD.difftf(cl,tf,ivtf);
    } else {
      edit_claimsetview();
    }      
  } else { // copy from feature diff field
    var ref= new claim_vtf(GetFeatureDiff(cl,tf));
    ref.setver(GetRefDiff());
    //alert("Import "+ref.getValue());
    featurechangeimp(cl,tf,ref.getValue());
  }
  document.getElementById("tfidiff_"+cl+"_"+tf).value=GetFeatureImport(cl,tf);
}

MOD_CLD.difftf=function(cl,tf,cltf) {
  var ref= new claim_vtf(cltf);
  //  GlobalGetFeatureAttr(ref.getver(),ref.getcl(),ref.gettf(),"desc");
  return diffString(GlobalGetFeatureAttr(ref.getver(),ref.getcl(),ref.gettf(),"desc"),GetFeatureDesc(cl,tf));
}


// compare feature with features of claims in set vers
MOD_CLD.findtfbestmatch=function(cl,tf) {
  var tfdesc1=GetFeatureDesc(cl,tf);
  var vers=GetRefDiff();
  var cl_iter;
  MOD_CLD._tfdiffcl= cl;
  MOD_CLD._tfdiffver=vers;
  MOD_CLD._tfdifftf=tf;

  var found=[];
  if(!vers || vers == GetClaimSetVersion()) {
    cl_iter=new claim_iterator(1,cl); // search only features before claim cl.
  } else {
    cl_iter=new claim_iterator(1,"",vers); 
  }
  while(!cl_iter.end()) {
    var tf_iter=new claim_features_iterator(cl_iter.getValue(),vers);
    while(!tf_iter.end()) {
      if(!tf_iter.getEq()) { // skip eq features
	var difob=new diffObj(cl+"."+tf,tf_iter.gettext(),tf_iter.getDesc(),tfdesc1);
	if(difob.streq>0){
	  found.push(difob);
	}
      }
      tf_iter.next();
    }
    cl_iter.next();
  }
  found.sort(MOD_CLD.sortmosteq);
  if(!vers || vers == GetClaimSetVersion()){
    if(found.length>10) {
      found=found.slice(1,11);// get rid of myself
    } else {
      found=found.slice(1,found.length);
    }
  } else if(found.length>10) {
    found=found.slice(0,10);
  }
  MOD_CLD._tfdiffObjs=found;
  edit_claimsetview();
}


MOD_CLD.tfdiff_onjointf=function(cl,tf){
  featuresjoin(cl,tf);
  edit_claimsetview();
}

MOD_CLD.tfdiff_onsplittf=function(cl,tf){
  var txtarea=document.getElementById("tftxtarea"+cl+"_"+tf);  
  var cursorat=html_getCursorPos(txtarea);
  if(!cursorat) {
    return;
  }
  var txt1=txtarea.value.substring(0,cursorat);
  var txt2=txtarea.value.substring(cursorat,txtarea.value.length); 
  featuresplit(cl,tf,txt1,txt2);
  edit_claimsetview();
}

MOD_CLD.tfdiff_onstripheader=function(cl) {
  var txtarea=document.getElementById("tftxtarea"+cl+"_1");  
  var cursorat=html_getCursorPos(txtarea);
 
  var txt1=txtarea.value.substring(0,cursorat); // header
  var txt2=txtarea.value.substring(cursorat,txtarea.value.length);

  SetClaimHeader(cl,txt1);
  SetFeatureDesc(cl,1,txt2);
  edit_claimsetview();	    
}

MOD_CLD.tfdiff_onjoinheader=function(cl) {
  SetFeatureDesc(cl,1,GetClaimHeader(cl)+" "+GetFeatureDesc(cl,1));
  SetClaimHeader(cl,"");
  edit_claimsetview();
}


//---------------------------------------------------------------------------------------
// 
//                           claim differences 
//
//---------------------------------------------------------------------------------------


MOD_CLD._findbestmatch=function(cl,vers) {
  var tfdesc1=GetClaimDescription(cl);
  var cldiff=1;
  var cllast=GetClaimSetLength(vers)+1;
  
  MOD_CLD._diffObjs=new Array();
  MOD_CLD._diffcl= cl;
  MOD_CLD._diffver=vers;
  
  // if comparing with same set, compare only with claims before cl
  if(vers == GetClaimSetVersion()) {
    cllast=cl;
  }

  while(cldiff < cllast) {
    MOD_CLD._diffObjs.push(new diffObj(cl,cldiff,GetClaimDescription(cldiff,vers),tfdesc1));
    cldiff++;
  }
  
}
  
MOD_CLD.findbestmatch=function(cl,vers) {
  MOD_CLD._findbestmatch(cl,vers);
  edit_claimsetview();
}  

function bestmatch_iterator(tab) {
  this.base=array_iterator;
  this.base(tab);
}
bestmatch_iterator.prototype=new array_iterator;
bestmatch_iterator.prototype.constructor=bestmatch_iterator;
bestmatch_iterator.prototype.getValue=function() { return this.getElem().cldiff;}


MOD_CLD.onchangeclaimdiffeq=function(cl,eq) {
  MOD_CLX._onchangeclaimeq(cl,eq);
  document.getElementById("ediff_"+cl).value=GetClaimEq(cl);
  //edit_claimsetview();
}


MOD_CLD.onchangecldiff=function(cl) {
  //SetClaimDiff(cl,diffver+"/"+html_selected("diffcl"+cl));
  SetClaimDiff(cl,GetRefDiff()+"/"+html_selected_iter("diffcl"+cl));

  var ref= new claim_vtf(GetClaimDiff(cl));
  if(!ref.getver()) {
    ref.setver(GetRefDiff());
  }
  document.getElementById("TDIFF"+cl).innerHTML=MOD_CLD.diffClaim(cl,ref.getcl(),ref.getver());
  if(document.getElementById("wimp"+cl)){
    setwhatis_IMP("wimp"+cl,GetRefDiff()+"/"+GetClaimDiff(cl));
  }
  //edit_claimsetview();	  
}

MOD_CLD.diffclimport=function(cl,icl,ver) {
  claim_import(cl,icl,ver,GetDossierLang());
  
  document.getElementById("idiff_"+cl).value=GetClaimImport(cl); // beautify 
  document.getElementById("ediff_"+cl).value=GetClaimEq(cl); // beautify 
  SetClaimDiff(cl,GetClaimImport(cl));

  var ref= new claim_vtf(GetClaimDiff(cl));
  if(!ref.getver()) {
    ref.setver(GetRefDiff());
  }

  document.getElementById("TDIFF"+cl).innerHTML=MOD_CLD.diffClaim(cl,ref.getcl(),ref.getver());
  CORRUPTEDDOS=1;
}

// do brute import
MOD_CLD.diffclreplace=function(cl,icl,ver) {
  claim_replace(cl,icl,ver,GetDossierLang());
  document.getElementById("idiff_"+cl).value=GetClaimImport(cl); // beautify 
  document.getElementById("ediff_"+cl).value=GetClaimEq(cl); // beautify 
  SetClaimDiff(cl,GetClaimImport(cl));
  var ref= new claim_vtf(GetClaimDiff(cl));
  if(!ref.getver()) {
    ref.setver(GetRefDiff());
  }
  document.getElementById("TDIFF"+cl).innerHTML=MOD_CLD.diffClaim(cl,ref.getcl(),ref.getver());
  CORRUPTEDDOS=1;
}

MOD_CLD.exitfindbest=function() {  
  MOD_CLD._diffcl="";
  MOD_CLD._diffver="";
  MOD_CLD._clclaimdict="";
  MOD_CLD._versclaimdict="";
  edit_claimsetview();	  
}


MOD_CLD.cldiff_import=function(cl) {
  SetClaimDiff(cl,GetRefDiff()+"/"+html_selected_iter("diffcl"+cl));
  MOD_CLD.diffclimport(cl,html_selected_iter("diffcl"+cl),GetRefDiff());
}

MOD_CLD.cldiff_replace=function(cl) {
  SetClaimDiff(cl,GetRefDiff()+"/"+html_selected_iter("diffcl"+cl));
  MOD_CLD.diffclreplace(cl,html_selected_iter("diffcl"+cl),GetRefDiff());
}

MOD_CLD.cldiff_merge=function(cl) {
  var mcl=html_selected_iter("diffcl"+cl);
  var mver=GetRefDiff();
  claim_merge(cl,mcl,mver,GetDossierLang());
  document.getElementById("TDIFF"+cl).innerHTML=MOD_CLD.diffClaim(cl,mcl,mver);
}


MOD_CLD.cldiff_seteq=function(cl) {
  if(GetRefDiff() == GetClaimSetVersion()) {
    MOD_CLD.onchangeclaimdiffeq(cl,html_selected_iter("diffcl"+cl));
  } else {
    alert("Cannot set equivalences between claims on different claimsets");
  }
}

MOD_CLD.diffClaim=function(cl,cldiff,vers) {
  // faster ?
  if(cl == cldiff && vers==GetClaimSetVersion()) {
    return GetClaimDescription(cl); // tfdesc1;
  }
  return diffString(GetClaimDescription(cldiff,vers),GetClaimDescription(cl));
}



  
function showdiffclaimcompact(cl,diff_iter){
   var clclass=ClaimTypeStyle(cl);
   var dep=GetClaimDep(cl);   
   var eq=GetClaimEq(cl);      
   var ref,refver=GetRefDiff();

   var html="";
   html+="<TR><TH class='"+clclass+" ' >Claim<br>"+cl+"<sup>"+GetClaimDep(cl)+"</sup></TH>";
   html+="<TH align=left width=40%>"; 

 
   var ref= new claim_vtf(GetClaimDiff(cl));
   // do not handle more than one version at a time
   if(!ref.getver() || ref.getver() != refver) { // if reference is to some other version, compare to myself
     ref.setver(refver);
   }

   ref.setcl(Math.min(ref.getcl(),ref.lastcl()));
   //alert(GetClaimDiff(cl)+" redirected to "+ref.gettext(cl));
    
   html+="vs. Claim ";
   
   // changed into a set with less claims, override with same claim number
   // compare to smaller set of claims , take last claim as default
   
   // expand sort commands for best match
   if(cl == MOD_CLD._diffcl && refver== MOD_CLD._diffver) {
     html+=html_selection_iter("diffcl"+cl,new bestmatch_iterator(MOD_CLD._diffObjs),ref.getcl(),
			       "MOD_CLD.onchangecldiff("+cl+");",""," ");
     html+=" ";
     mclass=(MOD_CLD._sortby=="=") ? "BUTSEL" : "NOBUTSEL";      
     html+=html_whatis("<input type=Button value='=' width=30 id='sorteq' class='"+mclass+"' onclick='MOD_CLD.sortbestmatch("+cl+",\"=\")'>","Sort by most in common");
     mclass=(MOD_CLD._sortby=="+") ? "INS BUTSEL" : "INS NOBUTSEL";
     html+=html_whatis("<input type=Button value='+' width=30 id='sortplus' class='"+mclass+"' onclick='MOD_CLD.sortbestmatch("+cl+",\"+\")'>",
		       "Sort by least additions");
     
     mclass=(MOD_CLD._sortby=="-") ? "DEL BUTSEL" : "DEL NOBUTSEL";
     html+=html_whatis("<input type=Button value='-' width=30 id='sortmin' class='"+mclass+"' onclick='MOD_CLD.sortbestmatch("+cl+",\"-\")'>",
		       "Sort by least deletions");
     
     mclass=(MOD_CLD._sortby=="%") ? "BUTSEL" : "NOBUTSEL";
     html+=html_whatis("<input type=Button value='%' width=30 id='sortper' class='"+mclass+"' onclick='MOD_CLD.sortbestmatch("+cl+",\"%\")'>",
		       "Sort by most in common (%)");
     
     html+=html_whatis(" <input type=Button value='X' class='NOBUTSEL XCLOSE' onclick='MOD_CLD.exitfindbest()'>","Close");
     
   } else if(cl == MOD_CLD._clclaimdict && refver== MOD_CLD._versclaimdict) {

     html+=html_selection_iter("diffcl"+cl,new clkeymatch_iterator(),ref.getcl(),
			       "MOD_CLD.onchangecldiff("+cl+");",""," ");
     html+=" K"; 
     html+=html_selection_iter("selkwcl",new keymatch_iterator(),MOD_CLD._currkw.toLowerCase(),
			       "MOD_CLD.onchangekw("+cl+");",""," ");
     html+=html_whatis(" <input type=Button value='X' class='NOBUTSEL XCLOSE' onclick='MOD_CLD.exitfindbest()'>","Close");
   } else {// natural order
     
     diff_iter[refver].reset();
     html+=html_selection_iter("diffcl"+cl,diff_iter[refver],ref.getcl(),
			       "MOD_CLD.onchangecldiff("+cl+");",""," ");              
     html+=" ";
     
     if(cl==1 && GetRefDiff()==GetClaimSetVersion()) {
       html+=html_whatis("<input type=Button value='?' class='NOBUTSEL' disabled >","Find matching claims");
     } else {
       html+=html_whatis("<input type=Button value='?' class='NOBUTSEL' onclick='MOD_CLD.findbestmatch("+cl+","+refver+")"+
			 ";MOD_CLD.sortbestmatch("+cl+",\""+MOD_CLD._sortby+"\")' >","Find matching claims");
     }
     html+=" "; 
     html+=html_whatis("<input type=Button value='K' class='NOBUTSEL' onclick='MOD_CLD.findbestkeyword("+cl+","+refver+")'>",
		       "Find keywords");
   }

   html+="</TH>";
   // EQ
   html+="<TH align=left width=40%  style='border-left-width:0;border-right-width:0;'>";      
   if(GetClaimSetVersion()==1 || GetRefDiff() == GetClaimSetVersion()) {
     html+=whatis_E_button("MOD_CLD.cldiff_seteq("+cl+")");
   } else {
     html+=whatis_E_button();
   }
   html+=" <input type=text size=3  value='"+GetClaimEq(cl)+"' id='ediff_"+cl+"' ";
   html+="onChange='MOD_CLD.onchangeclaimdiffeq("+cl+",this.value);'> ";

   // IMP      
   if(GetClaimSetVersion()>1 && (GetRefDiff() != GetClaimSetVersion())) {
     html+=whatis_REP_button(GetRefDiff()+"/"+GetClaimDiff(cl),
			     "MOD_CLD.cldiff_replace("+cl+")",
			     "wrep"+cl);	
     
     html+=whatis_IMP_button( GetRefDiff()+"/"+GetClaimDiff(cl),
			      "MOD_CLD.cldiff_import("+cl+")",
			      "wimp"+cl);
     html+=" <input type=text size=3 value='" + GetClaimImport(cl) + "' id='idiff_"+cl+"'  ";
     html+=" onChange='MOD_CLD.diffclimport("+cl+");'> ";
     
     if(GetPrefsDebugging()) { // not fully tested 
       html+=whatis_MRG_button(GetRefDiff()+"/"+GetClaimDiff(cl),
			       "MOD_CLD.cldiff_merge("+cl+")",
			       "mimp"+cl);
     }
     html+="</TH>";
   } else {
     html+=whatis_IMP_button()+" <input type=text size=3 disabled value='";
     html+=GetClaimImport(cl)+"' id='idiff_"+cl+"' >";
   }

   html+="</TH>";
   //html+="<TD align=center class='CLHEADER'>Differences";
   html+="<TD ALIGN=right style='border-left-width:0;'>";
   html+="<button name='exp' onclick='MOD_CLD.tf_swfullview("+cl+",1);'>"+html_img(fulldiff_img)+"</button>";
   html+="</TD></TR>";
   html+="<TR><TD></TD>";  
   html+="<TD colspan=3 valign=bottom class='DIFFTXT' id='TDIFF"+cl+"'>";      
  
   html+=MOD_CLD.diffClaim(cl,ref.getcl(),refver);     

   html+="<BR></TD></TR>";
   return html;
}

function showdiffclaimfull(cl){
  var clclass=ClaimTypeStyle(cl);
  var dep=GetClaimDep(cl);   
  var eq=GetClaimEq(cl);      
  var ref,refver=GetRefDiff();
  var html="";
  html+="<TR><TD class='"+clclass+"' >Claim<br>"+cl+"</TD>";
  html+="<TH align=center  class='CLHEADER' colspan=2>Feature Differences";
  html+="</TH><td align=RIGHT>";
  html+="<button name='exp' onclick='MOD_CLD.tf_swfullview("+cl+",0);'>"+html_img(compactdiff_img)+"</button></TD>";
  html+="</TD></TR>";
  if(GetClaimHeader(cl)) {
    html+="<TR><TD></TD><TD colspan=2>"+html_flexarea(GetClaimHeader(cl),"tf_onchangeheader("+cl+");",
					    "class='ROTXT' id='tftxtarea"+cl+"_0'",
					    70)+"</TD></TR>";
    //html.push("<TD><TABLE height=100%><TR><TD valign=top></TD></TR></TABLE></TD></TR>");
    
  }
  var tf_iter= new claim_features_iterator(cl);
  while(!tf_iter.end()) {
    var tf=tf_iter.gettf();
    html+="<TR><TH align=center rowspan=2>"+tf_iter.gettext()+"</TH>";
    var tfdiff=tf_iter.getDiff() || tf_iter.getEq() || tf_iter.getImp();
    //check tfdiff is OK
    if(tfdiff) {
      var vtf= new claim_vtf(tfdiff);
      if(!vtf.isOK()) {
	tfdiff="";
      }
    }
    html+="<TH align=left> vs. tf ";
    if(MOD_CLD._tfdiffcl==cl &&  MOD_CLD._tfdifftf==tf) {	  
      html+=html_selection_iter("_difftfcl",new bestmatch_iterator(MOD_CLD._tfdiffObjs),tfdiff,
				"MOD_CLD.onchangetfdiff("+cl+","+tf+")",""," ");
      html+=html_whatis(" <input type=Button value='X' class='NOBUTSEL XCLOSE'  onclick='MOD_CLD.exittffindbest()'>","Close");
    } else {
      html+="<input type=text value='"+tfdiff+"' size=4 ";
      html+="id='difftf_"+cl+"_"+tf+"' onchange='MOD_CLD.onchangetfdiff("+cl+","+tf+",this.value)'> ";
      html+=html_whatis("<input type=Button value='?' class='NOBUTSEL' onclick='MOD_CLD.findtfbestmatch("+cl+","+tf+","+refver+")'>",
			"Find matching features");
      html+=" ";
      html+=html_whatis("<input disabled type=Button value='K' class='NOBUTSEL' onclick='MOD_CLD.findtfbestkeyword("+cl+","+tf+","+refver+")'>",
			"Find keywords");
    }
    if(tfdiff) {
      html+="</TH><TH align=left colspan=2>";
    } else {
      html+="</TH><TH align=left>";
    }
    //tfeq 
    if(GetClaimSetVersion()==1 || GetRefDiff() == GetClaimSetVersion()) {
      html+=whatis_E_button("MOD_CLD.tfdiff_seteq("+cl+","+tf+")");
    } else {
      html+=whatis_E_button();
    }
    html+=" <input type=text  size=3 value='"+tf_iter.getEq()+"' id='diff_tfeq"+cl+"_"+tf+"' ";
    html+="onChange='MOD_CLD.tfdiff_seteq("+cl+","+tf+",this.value);'> ";
    
    //tfimp
    if(GetClaimSetVersion()>1 && (GetRefDiff() != GetClaimSetVersion())) {
      html+=whatis_IMP_button(GetRefDiff()+"/"+tf_iter.getDiff(),
			      "MOD_CLD.tfdiff_import("+cl+","+tf+")",
			      "wimp"+cl+"_"+tf);
      html+=" <input type=text size=4 value='" + tf_iter.getImp()+"' id='tfidiff_"+cl+"_"+tf+"'  ";
      html+=" onChange='MOD_CLD.tfdiff_import("+cl+","+tf+",this.value);'> ";
    } else {
      html+=whatis_IMP_button()+" <input type=text size=4 disabled value='";
      html+=tf_iter.getImp()+"' id='tfidiff_"+cl+"_"+tf+"' >";
    }
    html+="</TH></TR><TR>";	
    if(tfdiff) {
      html+="<TD colspan=3 class='DIFFTXT' id='tfdifftxt"+cl+"_"+tf+"'>"+MOD_CLD.difftf(cl,tf,tfdiff)+"</TD>";
      } else {
      html+="<TD class='RWTXT' colspan=2 align=right>";
      html+=html_splitflexarea(tf_iter.getDesc(),
			       "tf_onchangefeature("+cl+","+tf+");",
			       "class='"+getFeatureClass(cl,tf)+"' id='tftxtarea"+cl+"_"+tf+"'",
			       70,2)+"</TD><TD valign=top>";

      if(tf==1) {
	if(claim_isdependent(cl)) { // use pure dependency
	  if(!GetClaimHeader(cl)) {
	    html+="<button onClick='MOD_CLD.tfdiff_onstripheader("+cl+");'>"+
	      html_img(striphead_img,""," ALT='Strip header at cursor'")+"</button><BR>";
	  } else {
	    html+="<button onClick='MOD_CLD.tfdiff_onjoinheader("+cl+")'>"+
	      html_img(jointf_img,"","ALT='Undo strip header'")+"</button><BR>";	
	  }
	}
      } else if(tf>1) {
	html+="<button onClick='MOD_CLD.tfdiff_onjointf("+cl+","+tf+");'>"+html_img(jointf_img)+"</button><BR>";
      }
      html+="<button onClick='MOD_CLD.tfdiff_onsplittf("+cl+","+tf+");'>"+html_img(splittf_img)+"</button></TD>";
    }
    html+="</TD></TR>";
    tf_iter.next();
  }
  return html;
}

function showdiffallclaims(){
  var clclass;
  var diff_iter= new Array();
  var html="<TABLE border=1 class=SMALLTAB>";

  ACT_MOD=MOD_CLD;

  CURRENTVIEW="diffview";

  // generate list of claims 
  //var allvers_iter= new range_iterator(1,GetClaimSetVersion()); (HS)
  var allvers_iter= new range_iterator(1,GetClaimSetsLength());
  while(!allvers_iter.end()) {
    diff_iter[allvers_iter.getValue()]=new range_iterator(1,GetClaimSetLength(allvers_iter.getValue()));
    allvers_iter.next();
  }
  
  var pcl_iter=new PageClaim_Iterator("cldiff"+GetClaimSetVersion());  
  while(!pcl_iter.end()) {
    if(MOD_CLD.TFVIEW[pcl_iter.getValue()]) {
      html+=showdiffclaimfull(pcl_iter.getValue());
    } else {
      html+=showdiffclaimcompact(pcl_iter.getValue(),diff_iter);
    }
    pcl_iter.next();
  }
  return html;
  html+="</TABLE>";
  return html;
}

function printdiffview() {
  var ref;
  var html="<TABLE class='LISTTXTCL'>";
  var cl_iter =  new filteredclaims_iterator();
  var diffcl; 
  while(!cl_iter.end()) {
    ref= new claim_vtf(GetClaimDiff(cl_iter.getValue()));
    if(!ref.getver()) {
      ref.setver(GetRefDiff());
    }
    html+="<TR><TD VALIGN=top align=center class='LISTTXTCL'><b>"+cl_iter.getValue()+".</b> ";
    
    html+="(<i>"+ref.getver()+"/"+ref.getcl()+"</i>)"; 
    html+="</TD><TD VALIGN=top class='DIFFTXT' id='TDIFF"+cl_iter.getValue()+"'>";
    html+=MOD_CLD.diffClaim(cl_iter.getValue(),ref.getcl(),ref.getver())+"<BR>&nbsp</TD></TR>";
    cl_iter.next();
  }
  html+="</TABLE>";
  return html;
}

